/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"encoding/json"
	"fmt"
	"github.com/chanxuehong/wechat/mch/pay"
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/common"
	. "saas_sys_base/modules/eshop/service"
	. "saas_sys_base/modules/orders/models"
	. "saas_sys_base/modules/tenant/service"
	"time"
)

type OrdersService struct {
	// add your model here
	tbOrders      Orders
	tenantService TenantService //这里后续可以移到一个中间件中
	shopService   EShopService
}

func (t *OrdersService) ListOrders(keyword string, page, psize int, orderField, order string) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_orders", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			// map[string]string{"table": "tb_xx as t2", "on": "t2.xid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	if orderField != "" && order != "" {
		orderStr := fmt.Sprintf("t1.%s %s", orderField, order)
		queryMap["orders"] = []string{orderStr, "t1.cdate asc"}
	}

	return lego.QueryListByMap(queryMap)
}

func (t *OrdersService) ListOrdersByWho(who *common.Who, orderCode, userName, mobile, beginTime, endTime, productName string, orderType int, shopids []int64, page, psize int, orderField, order string, orderStatus int) ([]*OrderList, int64, error) {
	return t.tbOrders.ListOrdersByWho(who, orderCode, userName, mobile, beginTime, endTime, productName, orderType, shopids, page, psize, orderField, order, orderStatus)
}

func (t *OrdersService) ListOrdersByWhoForMall(who *common.Who, orderCode string, orderType int, shopid int64, page, psize int, orderField, order string, orderStatus int) ([]*Orders, int64, error) {
	return t.tbOrders.ListOrdersByWhoForMall(who, orderCode, orderType, shopid, page, psize, orderField, order, orderStatus)
}

/* 根据id获取记录
 * -----------------
 */
func (t *OrdersService) GetOrdersById(id int64) *Orders {
	return t.tbOrders.RecodeById(id)
}

func (t *OrdersService) GetOrdersByWho(who *common.Who, id int64) *Orders {
	return t.tbOrders.RecodeByWho(who, id)
}

/* 添加
 * -----------------
 */
func (t *OrdersService) SaveOrders(item2add *Orders) bool {
	return t.tbOrders.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *OrdersService) DelOrdersById(id int64) bool {
	return t.tbOrders.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *OrdersService) UpdateOrders(rc *Orders) bool {
	return t.tbOrders.UpdateRecode(rc)
}

func (t *OrdersService) PayAction(payNotify *pay.PayNotify) error {
	var who common.Who
	attachStr := payNotify.Attach
	err := json.Unmarshal([]byte(attachStr), &who)
	if err != nil {
		return err
	}

	orderId, err := utils.StringToInt64(payNotify.OutTradeNo)
	var order = t.GetOrdersById(orderId)
	if err != nil {
		lego.LogError(err) //接口调试后，一般很难会进入这个分支
		return err
	}

	order.IfPay = common.STATE_YES
	order.PayTime = time.Now()
	t.tbOrders.UpdateRecode(order)

	//店铺增加销售额
	var shop = t.shopService.GetEShopById(order.EShopId)
	t.shopService.AddEShopBalance(shop, order.RealAmount, common.Sale, common.In, common.STATE_YES, order.Id, "", "订单销售")

	return nil
}

func (t *OrdersService) TestPayAction(orderId int64) error {

	var order = t.GetOrdersById(orderId)
	order.IfPay = common.STATE_YES
	order.PayTime = time.Now()
	t.tbOrders.UpdateRecode(order)

	//店铺增加销售额
	var shop = t.shopService.GetEShopById(order.EShopId)
	t.shopService.AddEShopBalance(shop, order.RealAmount, common.Sale, common.In, common.STATE_YES, order.Id, "", "订单销售")

	return nil
}
